package com.cd.util;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class CheckRegister
{
	private ResultSet rs;
	private StringBuffer result;
	private ReturnResult returnResult;
	private int check = 1;
	private DBUtil dataBase;

	public void init()
	{
		result = new StringBuffer("");
		returnResult = new ReturnResult();
		check = 1;
		dataBase = new DBUtil();
	}

	public ReturnResult checkUsername(String username)
	{
		init();
		if (username.equals(""))
		{
			check = 0;
			result.append("用户名为空,");
		} else if (username.matches("[0-9a-zA-Z]{" + username.length() + "}") == false)
		{
			check = 0;
			result.append("用户名只能含有数字和字母,");
		} else if (username.length() < 6 || username.length() > 9)
		{
			check = 0;
			result.append("用户名长度不符合大于等于6位且小于10位的要求,");
		} else
		{
			try
			{
				rs = dataBase
						.query("SELECT username FROM user where username = '"
								+ username + "'");
				if (rs.next())
				{
					check = 0;
					result.append("用户名已存在,");
				}
				if (rs.isClosed() == false)
				{
					rs.close();
				}
			} catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
		returnResult.setCheck(check);
		returnResult.setStr(result.toString());
		dataBase.closeService();
		return returnResult;

	}

	public ReturnResult checkPassword(String password, String repassword)
	{
		init();
		if (password.length() == 0)
		{
			check = 0;
			result.append("密码为不能空,");
		} else
		{
			if (!password.equals(repassword))
			{
				check = 0;
				result.append("两次输入密码不一致,");
			} else if (password.length() < 6 || password.length() > 9)
			{
				check = 0;
				result.append("密码长度不符合大于等于6位且小于10位的要求,");
			} else
			{
				if (!password.matches(".*?[0-9].*?"))
				{
					check = 0;
					result.append("密码未包含数字,");
				}
				if (!password.matches(".*?[a-z].*?"))
				{
					check = 0;
					result.append("密码未包含小写字母,");
				}
				if (!password.matches(".*?[A-Z].*?"))
				{
					check = 0;
					result.append("密码未包含大写字母,");
				}
			}
		}
		returnResult.setCheck(check);
		returnResult.setStr(result.toString());
		return returnResult;
	}

	public ReturnResult checkNickname(String nickname)
	{
		init();
		// 验证昵称
		if (nickname.length() == 0)
		{
			check = 2;
		}  else if (nickname.length() > 10)
		{
			check = 0;
			result.append("昵称不符合最大长度为10的要求,");
		}
		else if ((nickname.matches(".*?[\u4e00-\u9fa5].*?")
				|| nickname.matches(".*?[0-9].*?") || nickname
					.matches(".*?[a-zA-Z].*?")) == false)
		{
			System.out.println("错误的昵称为: " + nickname);
			check = 0;
			result.append("昵称只能包含中文数字和字母,");
		}
		returnResult.setCheck(check);
		returnResult.setStr(result.toString());
		return returnResult;
	}

	public ReturnResult checkEmail(String email)
	{
		init();
		// 验证邮件地址
		if (email.length() == 0)
		{
			check = 0;
			result.append("邮箱地址不能为空,");
		} else if (!email.matches(".+?@.+?"))
		{
			check = 0;
			result.append("邮箱不符合###@###格式,");
		} else
		{
			//System.out.println("邮箱为 :" + email);
			try
			{
				// rs = dataBase.query("SELECT FORM user where email = " +
				// email);
				PreparedStatement psm = dataBase.getConnection()
						.prepareStatement(
								"SELECT email FROM user where email = ?");
				psm.setString(1, email);
				rs = psm.executeQuery();
				if (rs.next())
				{
					check = 0;
					result.append("该邮箱已存在,");
				}
				if (rs.isClosed() == false)
				{
					rs.close();
				}
				if (psm.isClosed() == false)
				{
					psm.close();
				}
			} catch (SQLException e)
			{
				e.printStackTrace();
			}
		}
		returnResult.setCheck(check);
		returnResult.setStr(result.toString());
		return returnResult;
	}

	public ReturnResult checkNumber(String number)
	{
		// 验证手机号码
		init();
		if(number.equals("")){
			check = 1;
			returnResult.setCheck(check);
			returnResult.setStr(result.toString());
			return returnResult;
		}
		if (!number.matches("\\d{11}"))
		{
			check = 0;
			result.append("手机号码不符合11位数字要求,");
		}
		returnResult.setCheck(check);
		returnResult.setStr(result.toString());
		return returnResult;
	}

	public ReturnResult checkSex(String sex)
	{
		// 验证性别
		init();
		if ((sex.equals("未知") || sex.equals("男") || sex.equals("女"))== false)
		{
			check = 0;
			result.append("性别不符合要求");
		}else{
			check = 1;
		}
		returnResult.setCheck(check);
		returnResult.setStr(result.toString());
		return returnResult;
	}
	
	

}
